package com.dl.base.databind

import android.graphics.drawable.Drawable
import android.text.Spanned
import android.view.animation.Animation
import android.widget.TextView
import androidx.databinding.BindingAdapter
import com.dl.base.extendsion.isNotNull
import com.dl.base.extendsion.placeholder

@BindingAdapter("text", "placeholder", requireAll = false)
fun text(textView: TextView, text: String?, placeholder: String?) {
    if (placeholder != null) {
        textView.text = text.placeholder(placeholder)
    } else {
        val placeholder1 = text.placeholder()
        textView.text = placeholder1
    }
}

@BindingAdapter("textSize", requireAll = false)
fun textSize(textView: TextView, size: Float) {
    if (size > 0) {
        textView.textSize = size
    }
}

@BindingAdapter("textNonNull", requireAll = false)
fun textNonNull(textView: TextView, text: String?) {
    if (text != null) {
        textView.text = text.placeholder()
    }
}

@BindingAdapter("textNonNull", requireAll = false)
fun textNonNull(textView: TextView, text: Spanned?) {
    if (text != null) {
        textView.text = text
    }
}

@BindingAdapter("animation", requireAll = false)
fun animation(textView: TextView, animation: Animation?) {
    if (animation != null) {
        textView.startAnimation(animation)
    } else {
        textView.animation = null
    }
}

@BindingAdapter("text", "placeholder", requireAll = false)
fun text(textView: TextView, text: Spanned?, placeholder: String?) {
    if (text != null) {
        textView.text = text
    } else {
        if (placeholder.isNotNull()) {
            textView.text = placeholder
        } else {
            textView.text = "——"
        }
    }
}

@BindingAdapter("drawableRight", requireAll = false)
fun drawableRight(textView: TextView, drawable: Drawable?) {
    if (drawable != null) {
        drawable.setBounds(0, 0, drawable.minimumWidth, drawable.minimumHeight)
        textView.setCompoundDrawables(null, null, drawable, null)
    }
}


